package com.sn.socket.serviceImpl;
import com.sn.socket.controller.Operat;
import com.sn.socket.mapping.OperationMapping;
import com.sn.socket.service.OperationService;
import com.sn.socket.utils.SockServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ProjectName: springboot_socket
 * @Package: com.sn.socket.serviceImpl
 * @ClassName: ${CLASS_NAME}
 * @Author: xiahuihui
 * @Description: ${description}
 * @Date: 2020/5/12 0012 10:39
 * @Version: 1.0
 */

@Service
public class OperationServiceImpl implements OperationService {

    @Resource
    private OperationMapping operationMapping;

    private static final Logger logger = LoggerFactory.getLogger(OperationServiceImpl.class);

//    private SockServer sockServer = SockServer.getInstance();



    @Override
    public void timelyTask(Operat operat) {

        operat.getDeviceId().forEach(deviceId ->{
            if (!deviceId.isEmpty()){
                try {
                    StringBuffer message = new StringBuffer();
                    message.append(operat.getCommand()).append("#").append(operat.getFrequency()).append("#").append(operat.getTime()).append("#").append("A");
                    logger.info(message.toString());
                    SockServer.clientTalbles.get(deviceId).getSendMessages().offerFirst(message.toString());
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String createTime = df.format(new Date());
                    operationMapping.insertRealTime(deviceId,operat.getCommand(),operat.getFrequency(),operat.getTime(),createTime);
                } catch (NullPointerException e) {
                    logger.error("该设备未连接，找不到该设备！！！！");

                }
            }

//            try {
//
//                nettyTcpClient.sendMsg(message);
//                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                String createTime = df.format(new Date());
//                operationMapping.insertRealTime(deviceId,operat.getCommand(),operat.getFrequency(),operat.getTime(),createTime);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }

        });

    }
}
